<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
//文章管理控制器
class ArcController extends CommonController{
	private $model;
	public function __init(){
		//调用CommonController里面的构造函数
		parent::__init();
		//实例化Arc模型
		$this->model=new \Common\Model\Arc;
		
	}
	//文章展示
	public function index(){
		//统计总条数
		$total=$this->model->join('category','category_cid','=','cid')->where("is_recycle=0")->count();
		//分页使用
		$page=Page::row(3)->make($total);
		View::with('page',$page);
		//select*from article join category on category_cid=cid;
		$data=$this->model->join('category','category_cid','=','cid')->limit(page::limit())->where("is_recycle=0")->get();
		//分配数据
		View::with('data',$data);
		View::make();
	}
	public function add(){
		if(IS_POST){
			//添加文章
			if(!$this->model->store()) View::error($this->model->getError());
			View::success('添加成功',U('index'));
		}
		//处理所属分类
		$model= new \Common\Model\Cate;
		$cateData=Data::tree($model->get(),'cname');
		View::with('cateData',$cateData);
		//处理文章标签
		$model=new \Common\Model\Tag;
		$tagData=$model->get();
		View::with('tagData',$tagData);
		View::make();
	}
	//删除到回收站
	public function del(){
		$this->model->where("aid=".Q('get.aid',0,'intval'))->save(array('is_recycle'=>1));
		View::success('删除成功',U('index'));
	}
	//回收站
	public function recycle(){
		//统计总条数
		$total=$this->model->join('category','category_cid','=','cid')->where("is_recycle=1")->count();
		//分页使用
		$page=Page::row(3)->make($total);
		View::with('page',$page);
		
		$data=$this->model->join('category','category_cid','=','cid')->limit(Page::limit())->where("is_recycle=1")->get();
		//分配数据
		View::with('data',$data);
		View::make();
	}
	//回收站删除
	public function realDel(){
		$aid=Q('get.aid',0,'intval');
		$this->model->where("aid={$aid}")->delete();
		$ArcData = new \Common\Model\ArcData;
		
		$ArcData->where("article_aid={$aid}")->delete();
		$ArcTag=new \Common\Model\ArcTag;
		$ArcTag->where("article_aid={$aid}")->delete();
		View::success('删除成功',U('index'));
	}
		//还原
	public function huanyuan(){
		$aid=Q('get.aid',0,'intval');
		$this->model->where("aid={$aid}")->save(array('is_recycle'=>0));
		View::success('还原成功',U('index'));
	}
	//编辑
	public function edit(){
		//检测是否有提交
		if(IS_POST){
			if(!$this->model->edit()) View::error($this->model->getError());
			View::success('编辑成功',U('index'));
		}
		//1处理所属分类
		//实例化cate类模型
		$model=new \Common\Model\Cate;
		$cateData=Data::tree($model->get(),'cname');
		View::with('cateData',$cateData);
		//2处理“文章标签”
		$model=new \Common\Model\Tag;
		$tagData=$model->get();
		View::with('tagData',$tagData);
		//3获得旧内容
		$aid=Q('get.aid',0,'intval');
		$oldData=$this->model->join('article_data','aid','=','article_aid')->where("aid=$aid")->find();
		View::with('oldData',$oldData);
		//4获得文章所选标签id
		$arcTagModel=new \Common\Model\ArcTag;	
		$tids=$arcTagModel->where("article_aid=$aid")->lists('tag_tid');
		//如果这篇文章没有标签，那么$tids为null，页面in_array判断就会报错
		$tids=$tids?$tids:array();
		View::with('tids',$tids);
		View::make();
	}
	
	
	
	
	
	
	
	
	
}






 ?>